package com.changgou.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/weixin/pay")
@CrossOrigin
public class WeixinPayController {

    @Autowired
    private WeixinPayService weixinPayService;

    @Value("${mq.pay.exchange.order}")
    private String exchange;
    @Value("${mq.pay.queue.order}")
    private String queue;
    @Value("${mq.pay.routing.key}")
    private String routing;

    @Autowired
    private RabbitTemplate rabbitTemplate;



//    @RequestMapping("/create/native")
//    public Result createNative(String outtradeno,String money){
//        Map<String,String> resultMap = weixinPayService.createNative(outtradeno, money);
//        return new Result(true, StatusCode.OK,"创建二维码预付订单成功！",resultMap);
//    }
    @RequestMapping(value = "/create/native")
    public Result createNative(@RequestParam Map<String,String> parameter){
       Map<String,String> resultMap =  weixinPayService.createNativeBySkill(parameter);
       return new Result(true,StatusCode.OK,"创建二维码成！",resultMap);
    }

    @GetMapping("/closePay")
    public Result closePay(@RequestParam Long id) throws Exception {
        Map<String, String> stringStringMap = weixinPayService.closePay(id);
        return new Result(true,StatusCode.OK,"success",stringStringMap);
    }

    /***
     * 查询支付状态
     * @param outtradeno
     * @return
     */
    @GetMapping(value = "/status/query")
    public Result queryStatus(String outtradeno){
        Map<String,String> resultMap = weixinPayService.queryPayStatus(outtradeno);
        return new Result(true,StatusCode.OK,"查询状态成功！",resultMap);
    }

    @RequestMapping("/notify/url")
    public String notifyUrl(HttpServletRequest request){
        InputStream inputStream;
        try {
            //读取支付回调数据
            inputStream = request.getInputStream();
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer))!=-1){
                outSteam.write(buffer,0,len);
            }
            outSteam.close();
            inputStream.close();
            //将支付回调数据转换成xml字符串
            String result = new String(outSteam.toByteArray(), "utf-8");
            Map<String, String> map = WXPayUtil.xmlToMap(result);

            String attach = map.get("attach");
            Map<String,String> parameters = JSON.parseObject(attach, Map.class);
            System.out.println("hhhhhhhheehehehhehehe================");
            System.out.println(parameters);

            //将消息发送给rabbitMQ
            rabbitTemplate.convertAndSend(parameters.get("exchange"),parameters.get("routingkey"), JSON.toJSONString(map));

            //响应数据设置
            //响应数据设置
            Map respMap = new HashMap();
            respMap.put("return_code","SUCCESS");
            respMap.put("return_msg","OK");
            return WXPayUtil.mapToXml(respMap);
        }catch (Exception e){
            e.printStackTrace();
            //记录错误日志
        }
        return null;
    }


}
